<?php 

/**
 * 导入类
 */
use OSS\OssClient;
use OSS\Core\OssException;

/**
 * 阿里云OSS操作类
 */
class AliyunOSS {

    /**
     * Undocumented function
     *
     * @param array $oss 配置数组
     * @param string $object 文件在bucket中的存储全路径
     * @param string $filepath 本地文件全路径
     * @return Section
     */
    public static function upload(array $oss, string $bucket, string $object, string $filepath) {
        try {
            $client = self::_getInstance($oss);
            $client->uploadFile($bucket, $object, $filepath);
            return Section::ok();
        } catch (OssException $e) {
            return Section::fail($e->getMessage());
        }
    }

    /**
     * 获取OSS的对象实例
     *
     * @param array $oss 配置数组
     * @return void
     */
    private static function _getInstance(array $oss) {
        return new OssClient(
            $oss['AccessKeyID'],
            $oss['AccessKeySecret'],
            $oss['endpoint']
        );
    }
}
